package com.xlabs.client;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.xlabs.client.ChromelessPlayer.PlayerListener;
import com.xlabs.client.ChromelessPlayer.PlayerState;

/**
 * The main entry point for Jeeqs.
 */
public class Jeeqs implements EntryPoint, PlayerListener
{


    /**
     * The main panel.
     */
    private DockPanel mainPanel;
    
    /**
     * The center panel.
     */
    private HorizontalPanel centerPanel;
    
	/**
	 * The left vertical panel in center panel.
	 */
    private VerticalPanel leftCenterVerticalPanel;

	/**
	 * Middle center vertical panel.
	 */
    private VerticalPanel middleCenterVerticalPanel;

    /**
     * youtube search widget.
     */
    private YoutubeAdapter youtubeAdapterWidget = new YoutubeAdapter("Player1", this);

    /**
     * The comment box.
     */
    private TextArea commentBox = new TextArea();

    /**
     * The comment panel.
     */
    private VerticalPanel commentPanel = new VerticalPanel();

    /**
     * Current video id.
     */
    private String currentVideoId;

    /**
     * Comment view panel.
     */
    private VerticalPanel questionView = new VerticalPanel();

    /**
     * Scrollbar for comments.
     */
    private Questions questions;

    /**
     * Schollbar for coversations.
     */
    private Conversation conversation = new Conversation();

    /**
     * header;
     */
    private Header header;

    /**
     * On Module Load.
     */
    public void onModuleLoad() {

        this.initializeOuterPanels();

        this.initializeHeader();

        this.initializeVideoPanel();

        this.initializeCommentingView();

        this.initializeQuestionPanel();

        this.loadFirstPageQuestions();
        
        // Associate the Main panel with the HTML host page.
        RootPanel.get("geekyPage").add(mainPanel);
    }

    /**
     * Initializes the header.
     */
    private void initializeHeader() {
        this.header = new Header();
        
        this.mainPanel.add(this.header, DockPanel.NORTH);
    }

    /**
     * Initializes comment view.
     */
    private void initializeCommentingView() {
        this.leftCenterVerticalPanel.add(this.questionView);
        this.middleCenterVerticalPanel.add(this.conversation);

        this.questions = new Questions(this.youtubeAdapterWidget, this.conversation);
        
        this.questionView.add(this.questions);
    }

    /**
     * Initializes video panel.
     */
    private void initializeVideoPanel() {
        VerticalPanel videoPanel = new VerticalPanel();

        HorizontalPanel controlButtonsLayout = new HorizontalPanel();
        
        HorizontalPanel paddingPanelStart = new HorizontalPanel();
        paddingPanelStart.setHeight("5px");
        
        HorizontalPanel paddingPanelEnd = new HorizontalPanel();
        paddingPanelEnd.setHeight("5px");

        VerticalPanel displayAndControls = new VerticalPanel();
        displayAndControls.addStyleName("Group");
        displayAndControls.add(this.youtubeAdapterWidget);
        displayAndControls.add(paddingPanelStart);
        displayAndControls.add(controlButtonsLayout);
        displayAndControls.add(paddingPanelEnd);
        videoPanel.add(displayAndControls);

        this.initializeControlButtons(controlButtonsLayout);

        videoPanel.add(this.commentPanel);
        
        this.leftCenterVerticalPanel.add(videoPanel);
    }

    /**
     * Initializes comment panel.
     */
    private void initializeQuestionPanel() {
    	
        HorizontalPanel paddingPanelSeparator = new HorizontalPanel();
        paddingPanelSeparator.setHeight("5px");

        commentBox.setPixelSize(600, 80);
        commentBox.setAccessKey('q');
        
        this.commentPanel.add(this.commentBox);
        
        Button submitQuestion = new Button("Ask Question");
        submitQuestion.addClickHandler(new ClickHandler()
		{
			
			public void onClick(ClickEvent event)
			{
                CommentingServiceAsync myService = (CommentingServiceAsync) GWT.create(CommentingService.class);

                AsyncCallback<Object> postCallback = new AsyncCallback<Object>() {
                    public void onFailure(Throwable throwable) {
                        commentBox.setText("Post Comment Failed!" + throwable + "\n" + commentBox.getText());
                        
                    }

                    public void onSuccess(Object o) {
                        commentBox.setText("");
                    }
                };

                double time = Math.max(youtubeAdapterWidget.getTime(), 0) ;
                myService.postQuestion(commentBox.getText(), (int) time, currentVideoId, postCallback);
			}
		});
        
        this.commentPanel.add(paddingPanelSeparator);
        this.commentPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
        this.commentPanel.add(submitQuestion);
    }

    /**
     * Loads all comments.
     */
    private void loadFirstPageQuestions() {
        this.questions.queryFirstPage();
    }

    /**
     * Initializes main panel.
     */
    private void initializeOuterPanels() {
        this.mainPanel = new DockPanel();
        mainPanel.setHorizontalAlignment(DockPanel.ALIGN_LEFT);
        
        this.centerPanel = new HorizontalPanel();
        this.mainPanel.add(this.centerPanel, DockPanel.CENTER);
        
        this.leftCenterVerticalPanel = new VerticalPanel();
        this.centerPanel.add(this.leftCenterVerticalPanel);
        
        this.middleCenterVerticalPanel = new VerticalPanel();
        this.centerPanel.add(this.middleCenterVerticalPanel);
    }

    /**
     * Initializes control buttons
     * @param controlButtonsLayout buttons layout.
     */
    private void initializeControlButtons(HorizontalPanel controlButtonsLayout) {
        Button startButton = new Button("Start", new ClickListener() {
            public void onClick(Widget widget) {
                currentVideoId = "JPyuH4qXLZ0";
                youtubeAdapterWidget.loadVideoById(currentVideoId, 0);
            }
        });

        Button pauseButton = new Button("Pause", new ClickListener() {
            public void onClick(Widget widget) {
                youtubeAdapterWidget.pauseVideo();
                commentBox.setText("Ask your question at time : " + youtubeAdapterWidget.getTime());
                commentBox.addClickListener(new ClickListener() {
                    public void onClick(Widget widget) {
                        commentBox.setText("");
                        commentBox.removeClickListener(this);
                        commentBox.setFocus(true);
                    }
                });
                commentBox.setFocus(true);
            }
        });

        Button playButton = new Button("Play", new ClickListener() {
            public void onClick(Widget widget) {
                youtubeAdapterWidget.playVideo();
            }
        });

        controlButtonsLayout.add(startButton);
        controlButtonsLayout.add(pauseButton);
        controlButtonsLayout.add(playButton);
    }

	public void onReady()
	{
		// TODO Auto-generated method stub
		
	}

	/**
	 * see {@link PlayerListener}{@link #onStateChange(PlayerState)}
	 */
	public void onStateChange(PlayerState state)
	{
		questions.onStateChange(state);
	}
}
